package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import model.Database;
import model.Email;
import model.Indirizzo;
import model.Telefono;
import personalize.MyFileChooser;
import controller.ActionModifyAccount;
import controller.EditAccountKeyListener;
import controller.Engine;
import controller.GhostTextField;

@SuppressWarnings("serial")
public final class ModifyAccount extends JFrame implements Observer{

	JPanel contenitore ;
	JPanel dati_account;
	JPanel contenitore2;
	JPanel firstcolumn;
	JPanel secondcolumn;
	JPanel thirdcolumn;
	JPanel fourthcolumn;
	JPanel dati_contatto;
	JPanel user_pan;
	JPanel sex;

	static JPanel telefono;
	static JPanel indirizzo;
	static JPanel email;
	static JPanel tel;
	static JPanel telContainer;
	static JPanel adr;
	static JPanel adrContainer;
	static JPanel eml;
	static JPanel emlContainer;

	JPanel buttons;

	public static JLabel usertxt;
	public static JTextField passtxt;
	public static JTextField firstname;
	public static JTextField secondname;
	static JTextField indirizzo_txt;
	static JTextField citta_txt;
	static JTextField provincia_txt;
	static JTextField cap_txt;
	static JTextField telefono_txt;
	static JTextField email_txt;
	public static JComboBox<String> sector_box;
	static String[] sectors = {"Ricerca e Sviluppo",
		"Produzione e Qualità",
		"Logistica",
		"Marketing",
		"Vendite",
		"Amministrazione e Finanza",
		"Personale e Organizzazione",
		"Sistemi Informativi",
		"Relazioni Esterne",
	"Admin"};

	static ButtonGroup group;
	static JRadioButton femmina;
	static JRadioButton maschio;

	static JFileChooser fc = new MyFileChooser();

	static JButton photo;
	JButton aggiungi_adr;
	JButton aggiungi_tel;
	JButton aggiungi_eml;
	JButton remove;
	JButton apply;
	JButton annulla;

	//public static Account NuovotempUtente = new Account(); //creo un account temporaneo
	static ActionListener action = new ActionModifyAccount();
	static KeyListener textControl = new EditAccountKeyListener();
	//static ModifyAccount r=null;
	static JLabel image = null;
	static File file = null;
	static JFrame modificaAccount = new JFrame("Modifica Profilo Utente");
	public ModifyAccount() {
		modificaAccount = this;
		image = Engine.setImage(Database.tempUtente.getPhoto(),125,125);
		image.setName(Database.tempUtente.getPhoto());
		if (image.getName().contains("sex_"))
			file = new File("resource"+File.separatorChar+File.separatorChar+Database.tempUtente.getPhoto());
		else
			switch (Database.USER){
			case "load14":{
				file = new File("D:"+ File.separatorChar + File.separatorChar
						+"drop"+ File.separatorChar+ File.separatorChar
						+"Dropbox"+ File.separatorChar+ File.separatorChar
						+"resource" + File.separatorChar+ File.separatorChar
						+"profile_img" + File.separatorChar+ File.separatorChar
						+ Database.tempUtente.getPhoto());
				break;
			}
			case "root":{
				file = new File("resource" + File.separatorChar
						+ File.separatorChar + "profile_img" + File.separatorChar
						+ File.separatorChar + Database.tempUtente.getPhoto());
				break;
			}
			}
		//file = new File("resource"+File.separatorChar+File.separatorChar+"profile_img"+File.separatorChar+File.separatorChar+);

		System.out.println("(MA102)photo---> "+file.getPath());
		if (Database.tempUtente.countObservers() != 0)
			Database.tempUtente.deleteObservers();
		Database.tempUtente.addObserver(this);
		this.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				Engine.restartHome("account");
			}
		});
		startMod();
	}

	@SuppressWarnings("unused")
	public void startMod(){

		contenitore = new JPanel();
		contenitore.setBackground(Color.white);
		dati_account = new JPanel();
		dati_account.setBackground(Color.white);
		contenitore2  = new JPanel();
		contenitore2.setBackground(Color.white);
		firstcolumn  = new JPanel();
		firstcolumn.setBackground(Color.white);
		secondcolumn = new JPanel();
		secondcolumn.setBackground(Color.white);
		thirdcolumn  = new JPanel();
		thirdcolumn.setBackground(Color.white);
		fourthcolumn = new JPanel();
		fourthcolumn.setBackground(Color.white);
		dati_contatto = new JPanel();
		dati_contatto.setBackground(Color.white);
		sex = new JPanel();
		sex.setBackground(Color.white);
		user_pan = new JPanel();
		user_pan.setBackground(Color.white);

		tel = new JPanel();
		tel.setBackground(Color.white);
		adr  = new JPanel();
		adr.setBackground(Color.white);
		eml = new JPanel();
		eml.setBackground(Color.white);

		buttons = new JPanel();
		buttons.setBackground(Color.white);

		usertxt = new JLabel(Database.tempUtente.getUsername());
		usertxt.setMinimumSize(new Dimension(200,30));
		usertxt.setPreferredSize(new Dimension(200,30));
		usertxt.setMaximumSize(new Dimension(200,30));

		passtxt = new JTextField(Database.tempUtente.getPassword(),17);
		passtxt.setName("passtxt");
		GhostTextField focus = new GhostTextField(passtxt,"Password");
		passtxt.setMinimumSize(new Dimension(200,30));
		passtxt.setPreferredSize(new Dimension(200,30));
		passtxt.setMaximumSize(new Dimension(200,30));
		passtxt.setName("passtxt");
		passtxt.addKeyListener(textControl);

		firstname = new JTextField(Database.tempUtente.getFirstname(),17);
		firstname.setName("firstname");
		GhostTextField focus1 = new GhostTextField(firstname,"TuoNome");
		firstname.setMinimumSize(new Dimension(200,30));
		firstname.setPreferredSize(new Dimension(200,30));
		firstname.setMaximumSize(new Dimension(200,30));
		firstname.setName("firstname");
		firstname.addKeyListener(textControl);

		secondname = new JTextField(Database.tempUtente.getSecondname(),17);
		secondname.setName("secondname");
		GhostTextField focus2 = new GhostTextField(secondname,"TuoCognome");
		secondname.setMinimumSize(new Dimension(200,30));
		secondname.setPreferredSize(new Dimension(200,30));
		secondname.setMaximumSize(new Dimension(200,30));
		secondname.addKeyListener(textControl);
		secondname.setName("secondname");

		sector_box = new JComboBox<String>();
		if (Database.Utente.getLevel()==512){
			sector_box.addItem(sectors[9]);
			sector_box.setSelectedItem(sectors[9]);
		}else
		{
			for (int i=0;i<9;i++)
				sector_box.addItem(sectors[i]);
			sector_box.setSelectedItem(sectors[Database.tempUtente.getLevel()]);
		}
		sector_box.setMinimumSize(new Dimension(200,30));
		sector_box.setPreferredSize(new Dimension(200,30));
		sector_box.setMaximumSize(new Dimension(200,30));
		sector_box.setName("sector");
		sector_box.addActionListener(action);


		photo = new JButton();
		String aggInd = "<html>"+"<p ALIGN=CENTER>"+"Aggiungi"+"<br>"+"Indirizzo"+"</p>"+"</html>";
		aggiungi_adr = new JButton(aggInd);
		String aggTel = "<html>"+"<p ALIGN=CENTER>"+"Aggiungi"+"<br>"+"Telefono"+"</p>"+"</html>";
		aggiungi_tel = new JButton(aggTel);
		String aggEml = "<html>"+"<p ALIGN=CENTER>"+"Aggiungi"+"<br>"+"Email"+"</p>"+"</html>";
		aggiungi_eml = new JButton(aggEml);
		remove = new JButton("Rimuovi account");
		apply = new JButton("Applica");
		annulla = new JButton("Annulla");

		group = new ButtonGroup();
		maschio = new JRadioButton();
		maschio.setActionCommand("0");
		maschio.setText("M");
		maschio.setName("M");
		maschio.addActionListener(action);
		femmina = new JRadioButton();
		femmina.setActionCommand("1");
		femmina.setText("F");
		femmina.setName("F");
		femmina.addActionListener(action);

		if (Database.tempUtente.getSex()==0)
			maschio.setSelected(true);
		else
			femmina.setSelected(true);
		group.add(maschio);
		group.add(femmina);


		sector_box.setToolTipText("Settore");
		firstname.setToolTipText("Nome");
		secondname.setToolTipText("Cognome");
		photo.setToolTipText("Clicca qui per aggiungere una foto");
		passtxt.setToolTipText("Immetti una password");

		//setto i nomi dei JButton per permettere il riconoscimento della action da effettuare
		photo.setName("photo");
		aggiungi_adr.setName("aggiungi_adr");
		aggiungi_tel.setName("aggiungi_tel");
		aggiungi_eml.setName("aggiungi_eml");
		remove.setName("remove");
		apply.setName("applica");
		annulla.setName("annulla");

		photo.addActionListener(action);
		aggiungi_adr.addActionListener(action);
		aggiungi_tel.addActionListener(action);
		aggiungi_eml.addActionListener(action);
		remove.addActionListener(action);
		apply.addActionListener(action);
		annulla.addActionListener(action);


		//costruisco la struttura del frame

		JLabel username_label = new JLabel("Username");
		username_label.setMinimumSize(new Dimension(70,30));
		username_label.setPreferredSize(new Dimension(70,30));
		username_label.setMaximumSize(new Dimension(70,30));
		JLabel password_label = new JLabel("Password");
		password_label.setMinimumSize(new Dimension(70,30));
		password_label.setPreferredSize(new Dimension(70,30));
		password_label.setMaximumSize(new Dimension(70,30));
		JLabel role_label = new JLabel("Ruolo");
		role_label.setMinimumSize(new Dimension(70,30));
		role_label.setPreferredSize(new Dimension(70,30));
		role_label.setMaximumSize(new Dimension(70,30));
		JLabel name_label = new JLabel("Nome");
		name_label.setMinimumSize(new Dimension(70,30));
		name_label.setPreferredSize(new Dimension(70,30));
		name_label.setMaximumSize(new Dimension(70,30));
		JLabel surname_label = new JLabel("Cognome");
		surname_label.setMinimumSize(new Dimension(70,30));
		surname_label.setPreferredSize(new Dimension(70,30));
		surname_label.setMaximumSize(new Dimension(70,30));
		JLabel sex_label = new JLabel("Sesso");
		sex_label.setMinimumSize(new Dimension(70,30));
		sex_label.setPreferredSize(new Dimension(70,30));
		sex_label.setMaximumSize(new Dimension(70,30));

		photo.setLayout(new BoxLayout(photo,BoxLayout.X_AXIS));
		photo.add(image);
		photo.setMargin(new Insets(5,0,5,0));
		photo.add(Box.createHorizontalStrut(0));

		sex.setLayout(new BoxLayout(sex,BoxLayout.X_AXIS));
		sex.add(Box.createHorizontalStrut(55));
		sex.add(maschio);
		sex.add(Box.createHorizontalStrut(30));
		sex.add(femmina);
		sex.add(Box.createHorizontalGlue());

		user_pan.setLayout(new BoxLayout(user_pan,BoxLayout.X_AXIS));
		user_pan.add(Box.createHorizontalStrut(12));
		user_pan.add(usertxt);
		user_pan.add(Box.createHorizontalGlue());

		firstcolumn.setLayout(new BoxLayout(firstcolumn,BoxLayout.Y_AXIS));
		firstcolumn.setAlignmentX(LEFT_ALIGNMENT);
		firstcolumn.add(username_label);
		firstcolumn.add(Box.createVerticalStrut(5));
		firstcolumn.add(password_label);
		firstcolumn.add(Box.createVerticalStrut(5));
		firstcolumn.add(role_label);
		firstcolumn.add(Box.createVerticalGlue());

		secondcolumn.setLayout(new BoxLayout(secondcolumn,BoxLayout.Y_AXIS));
		secondcolumn.setAlignmentX(LEFT_ALIGNMENT);
		secondcolumn.add(user_pan);
		secondcolumn.add(Box.createVerticalStrut(5));
		secondcolumn.add(passtxt);
		secondcolumn.add(Box.createVerticalStrut(5));
		secondcolumn.add(sector_box);
		secondcolumn.add(Box.createVerticalGlue());

		thirdcolumn.setLayout(new BoxLayout(thirdcolumn,BoxLayout.Y_AXIS));
		thirdcolumn.setAlignmentX(LEFT_ALIGNMENT);
		thirdcolumn.add(name_label);
		thirdcolumn.add(Box.createVerticalStrut(5));
		thirdcolumn.add(surname_label);
		thirdcolumn.add(Box.createVerticalStrut(5));
		thirdcolumn.add(sex_label);
		thirdcolumn.add(Box.createVerticalGlue());

		fourthcolumn.setLayout(new BoxLayout(fourthcolumn,BoxLayout.Y_AXIS));
		fourthcolumn.setAlignmentX(LEFT_ALIGNMENT);
		fourthcolumn.add(firstname);
		fourthcolumn.add(Box.createVerticalStrut(5));
		fourthcolumn.add(secondname);
		fourthcolumn.add(Box.createVerticalStrut(10));
		fourthcolumn.add(sex);
		fourthcolumn.add(Box.createVerticalGlue());

		contenitore2.setLayout(new BoxLayout(contenitore2,BoxLayout.X_AXIS));
		contenitore2.setAlignmentX(LEFT_ALIGNMENT);
		contenitore2.add(firstcolumn);
		contenitore2.add(secondcolumn);
		contenitore2.add(Box.createHorizontalStrut(20));
		contenitore2.add(thirdcolumn);
		contenitore2.add(fourthcolumn);

		dati_account.setAlignmentX(CENTER_ALIGNMENT);
		dati_account.add(Box.createHorizontalStrut(0));
		dati_account.add(photo);
		dati_account.add(Box.createHorizontalStrut(7));
		dati_account.add(contenitore2);
		dati_account.add(Box.createHorizontalStrut(1));

		dati_contatto.setLayout(new GridLayout(3,1));
		dati_contatto.add(setAdr());
		dati_contatto.add(setTel());
		dati_contatto.add(setEml());

		if (Database.Utente.getLevel()!=512)
			buttons.add(remove);
		buttons.add(apply);
		buttons.add(annulla);

		contenitore.setLayout(new BoxLayout(contenitore,BoxLayout.Y_AXIS));
		contenitore.add(dati_account);
		contenitore.add(dati_contatto);
		contenitore.add(buttons);


		//posizionatore della Home frame

		modificaAccount.setIconImage(Engine.immaginelog);
		modificaAccount.add(contenitore);
		modificaAccount.setVisible(true);

		modificaAccount.setPreferredSize(new Dimension (860,550));
		modificaAccount.pack();
		Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (schermo.width-860)/2;
		int y = (schermo.height-550)/2;
		modificaAccount.setTitle("Modifica Account");
		modificaAccount.setLocation(x,y);
		modificaAccount.validate();
		modificaAccount.requestFocus();
		modificaAccount.setResizable(false);

	}

	@SuppressWarnings("unused")
	public JPanel setAdr(){
		Vector<Indirizzo> VectIndirizzo = Database.tempUtente.getIndirizzo();
		adr.add(Box.createHorizontalStrut(7));
		adr.add(aggiungi_adr);
		aggiungi_adr.setPreferredSize(new Dimension(155,96));
		adr.add(Box.createHorizontalStrut(1));
		adrContainer = new JPanel();
		adrContainer.setMinimumSize(new Dimension(573,32));
		adrContainer.setBackground(Color.white);
		adrContainer.setLayout(new BoxLayout(adrContainer,BoxLayout.Y_AXIS));
		JPanel scrollContainer = new JPanel();
		scrollContainer.setBackground(Color.white);
		JScrollPane scroll= new JScrollPane(adrContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setPreferredSize(new Dimension(573, 96));
		scrollContainer.add(scroll);
		for (int i=0; i<VectIndirizzo.size();i++){
			indirizzo = new JPanel();
			indirizzo_txt = new JTextField(Database.tempUtente.getIndirizzo().get(i).getIndirizzo(),19);
			indirizzo_txt.setName("indirizzo_txt");
			GhostTextField focus = new GhostTextField(indirizzo_txt,"Via,Indirizzo,N°Civico");
			indirizzo_txt.addKeyListener(textControl);

			citta_txt = new JTextField(Database.tempUtente.getIndirizzo().get(i).getCitta(),14);
			citta_txt.setName("citta_txt");
			GhostTextField focus1 = new GhostTextField(citta_txt,"Ancona");
			citta_txt.addKeyListener(textControl);

			provincia_txt = new JTextField(Database.tempUtente.getIndirizzo().get(i).getProvincia(),2);
			provincia_txt.setName("provincia_txt");
			GhostTextField focus2 = new GhostTextField(provincia_txt,"AN");
			provincia_txt.addKeyListener(textControl);

			cap_txt = new JTextField(""+Database.tempUtente.getIndirizzo().get(i).getCap(),5);
			cap_txt.setName("cap_txt");
			GhostTextField focus3 = new GhostTextField(cap_txt,"65100");
			cap_txt.addKeyListener(textControl);

			//inserisce il bottone per eliminare l'indirizzo
			JButton adr_button = new JButton("X");
			adr_button.setPreferredSize(new Dimension(30,30));
			adr_button.setMargin(new Insets(0,-5,0,-5));
			adr_button.addActionListener(action);
			Component comp = new Component(){
			};
			comp.setName(Integer.toString(VectIndirizzo.get(i).getAdr_id()));
			adr_button.add(comp);
			adr_button.setName("adr_rimuovi");

			indirizzo.setMaximumSize(new Dimension(573,30));
			indirizzo.setLayout(new BoxLayout(indirizzo,BoxLayout.X_AXIS));
			indirizzo.setBackground(Color.white);
			indirizzo.add(indirizzo_txt);
			indirizzo.add(Box.createHorizontalStrut(0));
			indirizzo.add(citta_txt);
			indirizzo.add(Box.createHorizontalStrut(0));
			indirizzo.add(provincia_txt);
			indirizzo.add(Box.createHorizontalStrut(0));
			indirizzo.add(cap_txt);
			indirizzo.add(Box.createHorizontalStrut(1));
			indirizzo.add(adr_button);
			adrContainer.add(indirizzo);
		}
		adr.add(scrollContainer);
		adr.add(Box.createHorizontalGlue());
		return adr;
	}

	@SuppressWarnings("unused")
	public JPanel setTel(){
		Vector<Telefono> VectTelefono = Database.tempUtente.getTelefono();
		tel.add(Box.createHorizontalStrut(7));
		tel.add(aggiungi_tel);
		aggiungi_tel.setPreferredSize(new Dimension(155,96));
		tel.add(Box.createHorizontalStrut(1));
		telContainer = new JPanel();
		telContainer.setBackground(Color.white);
		telContainer.setLayout(new BoxLayout(telContainer,BoxLayout.Y_AXIS));
		telContainer.setMinimumSize(new Dimension(573,32));
		JPanel scrollContainer = new JPanel();
		scrollContainer.setBackground(Color.white);
		JScrollPane scroll= new JScrollPane(telContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setPreferredSize(new Dimension(573, 96));
		scrollContainer.add(scroll);
		for (int i=0; i<VectTelefono.size();i++){
			telefono = new JPanel();
			telefono_txt = new JTextField(Database.tempUtente.getTelefono().get(i).getNum(),20);
			telefono_txt.setName("telefono_txt");
			GhostTextField focus = new GhostTextField(telefono_txt,"000-1234567");
			//inserisce il bottone per eliminare il telefono
			JButton tel_button = new JButton("X");
			tel_button.setPreferredSize(new Dimension(30,30));
			tel_button.setMargin(new Insets(0,-5,0,-5));
			tel_button.addActionListener(action);
			Component comp = new Component(){

			};
			comp.setName(Integer.toString(VectTelefono.get(i).getTel_id()));
			tel_button.add(comp);
			tel_button.setName("tel_rimuovi");

			telefono.setMaximumSize(new Dimension(573,30));
			telefono.setLayout(new BoxLayout(telefono,BoxLayout.X_AXIS));
			telefono.setBackground(Color.white);
			telefono.add(telefono_txt);
			telefono.add(Box.createHorizontalStrut(1));
			telefono.add(tel_button);
			telContainer.add(telefono);
		}
		tel.add(scrollContainer);
		tel.add(Box.createHorizontalGlue());
		return tel;
	}

	@SuppressWarnings("unused")
	public JPanel setEml(){
		Vector<Email> VectEmail= Database.tempUtente.getEmail();
		eml.add(Box.createHorizontalStrut(7));
		eml.add(aggiungi_eml);
		aggiungi_eml.setPreferredSize(new Dimension(155,96));
		eml.add(Box.createHorizontalStrut(1));
		emlContainer = new JPanel();
		emlContainer.setBackground(Color.white);
		emlContainer.setLayout(new BoxLayout(emlContainer,BoxLayout.Y_AXIS));
		emlContainer.setMinimumSize(new Dimension(573,32));
		JPanel scrollContainer = new JPanel();
		scrollContainer.setBackground(Color.white);
		JScrollPane scroll= new JScrollPane(emlContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.setPreferredSize(new Dimension(573, 96));
		scrollContainer.add(scroll);
		for (int i=0; i<VectEmail.size();i++){
			email = new JPanel();
			email_txt = new JTextField(VectEmail.get(i).getEmail(),20);
			email_txt.setName("email_txt");
			GhostTextField focus = new GhostTextField(email_txt,"tuaMail@LoAD.com");
			//inserisce il bottone per eliminare l'email
			JButton eml_button = new JButton("X");
			eml_button.setPreferredSize(new Dimension(30,30));
			eml_button.setMargin(new Insets(0,-5,0,-5));
			eml_button.addActionListener(action);
			Component comp = new Component(){

			};
			comp.setName(Integer.toString(VectEmail.get(i).getEml_id()));
			eml_button.add(comp);
			eml_button.setName("eml_rimuovi");

			email.setMaximumSize(new Dimension(573,30));
			email.setLayout(new BoxLayout(email,BoxLayout.X_AXIS));
			email.setBackground(Color.white);
			email.add(email_txt);
			email.add(Box.createHorizontalStrut(1));
			email.add(eml_button);
			emlContainer.add(email);
		}
		eml.add(scrollContainer);
		eml.add(Box.createHorizontalGlue());
		return eml;
	}

	public static void setPhoto() {
		int returnValue = fc.showOpenDialog(new JFrame());
		if (returnValue == JFileChooser.APPROVE_OPTION){
			file = fc.getSelectedFile();
			image = Engine.setImage(file.toPath(),125,125);
			image.setName(file.getName());
		}
	}

	public static void stopModifyAccount() {
		modificaAccount.dispose();
	}

	public static void allertInvalidPassword() {
		JOptionPane.showMessageDialog(modificaAccount,"Il campo password deve contenere almeno 4 caratteri!");
	}
	public static void allertInvalidName() {
		JOptionPane.showMessageDialog(modificaAccount,"Il campo nome deve contenere almeno 2 caratteri!");
	}
	public static void allertInvalidSurname() {
		JOptionPane.showMessageDialog(modificaAccount,"Il campo cognome deve contenere almeno 2 caratteri!");
	}

	public static void allertConfirmRemove() {
		//JOptionPane.showConfirmDialog(modificaAccount, "Il campo cognome è vuoto!");
		int res = JOptionPane.showConfirmDialog(modificaAccount, "Sei sicuro di voler rimuovere l'account?","ATTENZIONE!",JOptionPane.YES_NO_OPTION);
		if (res==0)
			Engine.removeAccount();
	}

	public static void modifyAccountProp(){
		/*System.out.println("Prima: ");
		System.out.println("ps: "+ Database.Utente.getPassword());
		System.out.println("fn: "+ Database.Utente.getFirstname());
		System.out.println("sn: "+ Database.Utente.getSecondname());
		System.out.println("pn: "+ Database.Utente.getSector());
		System.out.println("ph: "+ Database.Utente.getPhoto());
		System.out.println("sx: "+ Database.Utente.getSex());
		for (int i = 0;i<Database.Utente.getIndirizzo().size();i++)
			System.out.println("adr"+i+": "+ Database.Utente.getIndirizzo().get(i).getIndirizzo()+" "+Database.Utente.getIndirizzo().get(i).getCitta()+" "+Database.Utente.getIndirizzo().get(i).getProvincia()+" "+Database.Utente.getIndirizzo().get(i).getCap());
		for (int i = 0;i<Database.Utente.getEmail().size();i++)
			System.out.println("eml"+i+": "+Database.Utente.getEmail().get(i).getEmail());
		for (int i = 0;i<Database.Utente.getTelefono().size();i++)
			System.out.println("tel"+i+": "+Database.Utente.getTelefono().get(i).getNum());*/
		Database.Utente.setPassword(Database.tempUtente.getPassword());
		Database.Utente.setFirstname(Database.tempUtente.getFirstname());
		Database.Utente.setSecondname(Database.tempUtente.getSecondname());
		if (Database.Utente.getLevel()!=512)
			Database.Utente.setLevel(Database.tempUtente.getLevel());
		Database.Utente.setPhoto(Database.tempUtente.getPhoto());
		Database.Utente.setSex(Database.tempUtente.getSex());
		Database.Utente.setIndirizzo(Database.tempUtente.getIndirizzo());
		Database.Utente.setTelefono(Database.tempUtente.getTelefono());
		Database.Utente.setEmail(Database.tempUtente.getEmail());
		/*System.out.println("Dopo: ");
		System.out.println("ps: "+ Database.Utente.getPassword());
		System.out.println("fn: "+ Database.Utente.getFirstname());
		System.out.println("sn: "+ Database.Utente.getSecondname());
		System.out.println("pn: "+ Database.Utente.getSector());
		System.out.println("ph: "+ Database.Utente.getPhoto());
		System.out.println("sx: "+ Database.Utente.getSex());
		for (int i = 0;i<Database.Utente.getIndirizzo().size();i++)
			System.out.println("adr"+i+": "+ Database.Utente.getIndirizzo().get(i).getIndirizzo()+" "+Database.Utente.getIndirizzo().get(i).getCitta()+" "+Database.Utente.getIndirizzo().get(i).getProvincia()+" "+Database.Utente.getIndirizzo().get(i).getCap());
		for (int i = 0;i<Database.Utente.getEmail().size();i++)
			System.out.println("eml"+i+": "+Database.Utente.getEmail().get(i).getEmail());
		for (int i = 0;i<Database.Utente.getTelefono().size();i++)
			System.out.println("tel"+i+": "+Database.Utente.getTelefono().get(i).getNum());
		 */
	}

	public static void confirmTempAccountProp(boolean flag) {

		//System.out.println("(MA492)photo---> "+image.getName());
		if (passtxt.getForeground()!=Color.lightGray)
			Database.tempUtente.setPassword(passtxt.getText());
		if (firstname.getForeground()!=Color.lightGray)
			Database.tempUtente.setFirstname(firstname.getText());
		if (secondname.getForeground()!=Color.lightGray)
			Database.tempUtente.setSecondname(secondname.getText());
		Database.tempUtente.setLevel(Integer.valueOf(sector_box.getSelectedIndex()));
		Database.tempUtente.setSex(Integer.valueOf(group.getSelection().getActionCommand()));
		if (image.getName().contains("sex_m") || image.getName().contains("sex_f"))
			if (group.getSelection().getActionCommand().contentEquals("0"))
				Database.tempUtente.setPhoto("sex_m.gif");
			else
				Database.tempUtente.setPhoto("sex_f.gif");
		else
			Database.tempUtente.setPhoto(image.getName());
		if (!Database.tempUtente.getIndirizzo().isEmpty())
			for (int i = 0; i<adrContainer.getComponentCount();i++){
				//int adr_id = Database.tempUtente.getIndirizzo().get(i).getAdr_id();
				String ind;
				String cit;
				String pro;
				String cap;
				if (((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(0)).getForeground()!=Color.lightGray){
					ind = ((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(0)).getText();
					Database.tempUtente.getIndirizzo().get(i).setIndirizzo(ind);
				}
				if (((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(2)).getForeground()!=Color.lightGray){
					cit = ((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(2)).getText();
					Database.tempUtente.getIndirizzo().get(i).setCitta(cit);
				}
				if (((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(4)).getForeground()!=Color.lightGray){
					pro = ((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(4)).getText();
					Database.tempUtente.getIndirizzo().get(i).setProvincia(pro);
				}
				if (((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(6)).getForeground()!=Color.lightGray){
					cap = ((JTextField)((JPanel) adrContainer.getComponent(i)).getComponent(6)).getText();
					Database.tempUtente.getIndirizzo().get(i).setCap(cap);
					//System.out.println("indirizzi: "+adr_id+" "+i);
				}
			}

		if (!Database.tempUtente.getTelefono().isEmpty())
			for (int i = 0; i<telContainer.getComponentCount();i++){
				//int tel_id = Database.tempUtente.getTelefono().get(i).getTel_id();
				String num;
				if (((JTextField)((JPanel) telContainer.getComponent(i)).getComponent(0)).getForeground()!=Color.lightGray){
					num = ((JTextField)((JPanel) telContainer.getComponent(i)).getComponent(0)).getText();
					Database.tempUtente.getTelefono().get(i).setNum(num);
				}
				//System.out.println("telefono: "+tel_id+" "+i);
			}
		if (!Database.tempUtente.getEmail().isEmpty())
			for (int i = 0; i<emlContainer.getComponentCount();i++){
				//int eml_id = Database.tempUtente.getEmail().get(i).getEml_id();
				String posta;
				if (((JTextField)((JPanel) emlContainer.getComponent(i)).getComponent(0)).getForeground()!=Color.lightGray){
					posta = ((JTextField)((JPanel) emlContainer.getComponent(i)).getComponent(0)).getText();
					Database.tempUtente.getEmail().get(i).setEmail(posta);
				}
				//System.out.println("email: "+eml_id+" "+i);
			}
		if (flag){
			String ph_ext = "";
			for (int i=0;i<image.getName().length();i++)
				if (image.getName().charAt(i)=='.'){
					for (int j=i;j<image.getName().length();j++)
						ph_ext += image.getName().charAt(j);
					break;
				}
			Database.tempUtente.setPhoto(Database.tempUtente.getUsername()+ph_ext);
			modifyAccountProp();
			if (!image.getName().contains(Database.tempUtente.getPhoto()))
				Engine.copyImageInResource(file);
		}
	}

	public static void Uppdater() {
		//System.out.println("Richiesta Aggiornamento Model");
		Database.tempUtente.uppdater();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		//System.out.println("Aggiornamento View Effettuato");
		remove(contenitore);
		validate();
		startMod();
	}


}

